/****************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsupprsapi.p                                    */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : supplier required export Process                 */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                 1-Jun-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i new}
{xxcimload.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i sosodxr}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

/*

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.
*/

DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.
define variable v_infile      as character        format "X(60)"  initial "/home/mfg/work/rs2379.csv"   no-undo.
define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.
define variable vc_order              as character                no-undo.

define variable shipfrom_from         as character  no-undo.
define variable shipfrom_to           as character  no-undo.
define variable shipto_from           as character  no-undo.
define variable shipto_to             as character  no-undo.
define variable part_from             as character  no-undo.
define variable part_to               as character  no-undo.
define variable order_from            as character  no-undo.
define variable order_to              as character  no-undo.
define variable current_ind           as logical  initial yes  no-undo.
define variable date_from             as date    no-undo.
define variable date_to               as date    no-undo.
define variable schtype               as integer initial 4   no-undo.
define variable prior_disp_qty         as decimal    no-undo.
define variable net_req               as decimal    no-undo.
define variable cum_plan_qty          as decimal    no-undo.
define variable prior_net_req         as decimal    no-undo.
define variable prior_contr           as character  no-undo.
define variable prior_part            as character  no-undo.
define variable disp_qty              as decimal    no-undo.

define buffer b_schmstr for sch_mstr.
define buffer b_pomstr for po_mstr.
define buffer b_poddet for pod_det.
define buffer b_scxref for scx_ref.

define temp-table ttSearchCondition no-undo

        field supplier_from         like creditor.creditorcode
        field supplier_to           like creditor.creditorcode
        field shipto_from         as character
        field shipto_to           as character
        field part_from             as character
        field part_to               as character
        field order_from            as character
        field order_to              as character
        field schtype               as integer
        field date_from             as date 
        field date_to               as date 
        field active_rls            as logical
        .


 define temp-table ttsupplierrs no-undo

        field schd_site      like si_mstr.si_site
        field schd_order     like scx_ref.scx_order
        field schd_line      like scx_ref.scx_line 
        field schd_vend      like po_mstr.po_vend
        field schd_shipto    like scx_ref.scx_shipto
        field schd_dock      like pod_det.pod_dock
        field schd_part      like pod_det.pod_part
        field schd_vpart     like pod_det.pod_part
        field schd_rlse_id   like sch_mstr.sch_rlse_id
        field schd_date      like schd_det.schd_date
        field schd_time      as   character
        field schd_reference like schd_det.schd_reference
        field schd_ord_mult  like pod_det.pod_ord_mult
        field schd_um        like pod_det.pod_um 
        field schd_pkg_code  like pod_det.pod_pkg_code
        field schd_pcr_qty   like sch_mstr.sch_pcr_qty
        field schd_interval  like schd_det.schd_interval
        field schd_pcs_date  like sch_mstr.sch_pcs_date
        field schd_scx_po    like scx_ref.scx_po
        field schd_cr_date   like sch_mstr.sch_cr_date
        field schd_cr_time   as   character
        field schd_eff_start like sch_mstr.sch_eff_start
        field schd_eff_end   like sch_mstr.sch_eff_end 
        field schd_fc_qual   like schd_det.schd_fc_qual
        field schd_qty       as   decimal
       
        

        index idx1 schd_order schd_line schd_date schd_time.

                    

  
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dssupplierrs FOR ttsupplierrs.

   hresult = dataset dssupplierrs:HANDLE.    


/** Program form  **/   

form
   
   v_infile                                colon 25  skip
   v_import                                colon 25  skip(2)
   
  
with frame a width 120 side-labels .
setframelabels(frame a:handle).

mainloop:
repeat on error undo mainloop, retry mainloop:

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "rs/"
       m_archpath    = m_archpath + "rs/"
       m_successpath = m_successpath + "rs/"
       m_errorpath   = m_errorpath + "rs/"
       m_workpath    = m_workpath + "rs/".


   empty temp-table ttsupplierrs.
   empty temp-table api_status.
/*
      /* Load item master data from JSON  */ 
      vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
      run loadjsonfile(input vc_inputjson,
                       input vc_outputjson,
                       output vl_RetOK).
      if vl_RetOK then
      do:
         
      end.

  /* Create CIM-LOAD file and do cim-load */
  find first ttsupplierrs no-error.
         assign vc_traceid = ttsupplierrs.abs_traceid.
         
  v_cim = string(vc_traceid).
  v_log = string(vc_traceid).
  */
 output to /home/mfg/work/cim/xxsupplierrsapi.log  .      
 
 if c-application-mode <> 'web':u
   then do:
      
      update
      v_infile
      v_import
      with frame a.
   end.
   
   input from value(v_infile).
   repeat:
   
   import unformatted l_line.
  
   vc_order = entry(1,l_line).
   if entry(4,l_line) = "2379" then substring(vc_order,3,1) = "D".
   
   
   find first pod_det where pod_det.pod_nbr = vc_order
                        and pod_det.pod_part = trim(entry(2,l_line))
                        and pod_det.pod_domain = global_domain
                        no-lock no-error.
   
   if avail pod_Det then 
   do:
   create ttsupplierrs.
   
   ttsupplierrs.schd_order  = vc_order.
   
  
   
   assign 
         
          ttsupplierrs.schd_part        = entry(2,l_line)
          ttsupplierrs.schd_vend        = entry(3,l_line)
          ttsupplierrs.schd_shipto      = entry(4,l_line)
          ttsupplierrs.schd_line        = pod_det.pod_line
          ttsupplierrs.schd_rlse_id     = entry(6,l_line)
          ttsupplierrs.schd_pcs_date    = date(entry(7,l_line)) 
          ttsupplierrs.schd_eff_start   = date(entry(8,l_line))
          /*
          ttsupplierrs.schd_eff_end     = entry(1,l_line)
          */
          ttsupplierrs.schd_date        = date(entry(9,l_line))
          ttsupplierrs.schd_qty         = dec(entry(10,l_line))
          ttsupplierrs.schd_fc_qual     = entry(11,l_line)
          /*
          ttsupplierrs.schd_reference   = entry(12,l_line)
          */
          .
          
           
   end. /* if avail pod_Det */
   
   
   end. /* repeat: */
 
 
 
 
 
if v_import then do:  

  /* create cim load format */
  cimloadprocess:
  do transaction on error undo cimloadprocess, leave cimloadprocess: 
  
  for each ttsupplierrs  break by ttsupplierrs.schd_order
                              by ttsupplierrs.schd_part.
         
   if first-of(ttsupplierrs.schd_part) then do:

    v_cim = m_workpath + "SPRS" + "_" + string(ttsupplierrs.schd_order) + "_" + string(ttsupplierrs.schd_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
    v_log = m_workpath + "SPRS" + "_" + string(ttsupplierrs.schd_order) + "_" + string(ttsupplierrs.schd_part) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    output stream f_cim to value(v_cim).

   
    
    put stream f_cim  unformatted 
            v_quote ttsupplierrs.schd_order v_quote v_space                    /*  purchase order  */
            v_quote ttsupplierrs.schd_part  v_quote v_space                    /*  part  */
            v_quote ttsupplierrs.schd_vend  v_quote v_space                    /*  vendor */
            v_quote ttsupplierrs.schd_shipto  v_quote v_space                  /*  ship to  */
            v_quote ttsupplierrs.schd_line  v_quote v_space                    /*  line  */
                   skip                                                        /*   */
            v_quote ttsupplierrs.schd_rlse_id  v_quote skip                    /*  release id  */       
            "N"  v_space                                                       /*  comment  */       
            v_default v_space                                                  /*  pcr quantity  */       
            v_quote ttsupplierrs.schd_pcs_date  v_quote  v_space                       /* pcr quantity date  */       
            v_quote ttsupplierrs.schd_eff_start  v_quote   v_space                     /* active start date  */       
            v_default v_space                                                  /* active end date  */       
            
            skip                                                                
            
             .	                 
	  end. /* if first-of (ttsupplierrs.schd_part) then do:*/
     
      put stream f_cim  unformatted 
            v_quote ttsupplierrs.schd_date v_quote v_space                    /*  date   */
            v_default v_space                                                 /*  time  */
            v_default v_space     skip                                            /*  reference  */
            v_quote ttsupplierrs.schd_qty v_quote v_space                    /*  quantity  */
            v_quote ttsupplierrs.schd_fc_qual v_quote skip.                    /*  quantity type  */
     
	   
   if last-of(ttsupplierrs.schd_part) then do:
   
   find pod_det where pod_Det.pod_nbr = ttsupplierrs.schd_order
                  and pod_det.pod_curr_rlse_id[1] = ttsupplierrs.schd_rlse_id
                  and pod_det.pod_line = ttsupplierrs.schd_line
                  and pod_det.pod_domain = global_domain
                  no-lock no-error.
   
   
    put stream f_cim  unformatted  
                           "." skip 
                           v_default v_space skip.
                if not available pod_det then 
                   put stream f_cim  unformatted  
                           "yes" skip.
                      put stream f_cim  unformatted  
                           "."    skip     .                              
                                
    output stream f_cim close.
    
     /* cim load process */
     v_runproagram = "rssmt.p".
     execname = "xxsupprsimptap.p".

     
      
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input ttsupplierrs.schd_order + "-" + ttsupplierrs.schd_part ,
                          input ttsupplierrs.schd_order + "-" + ttsupplierrs.schd_part,
                          output v_errstring,
                          output v_errorstatus) .
                 
       v_toterrorstatus = v_toterrorstatus and v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                 
       end. /* do vi_filecount = 1 to 2: */   
     end. /* if last-of */
     
    end. /* for each ttptp  */

     if  v_toterrorstatus then 

       undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */

   
         


   end. /* if vc_import */
   
        /* Handle cimload related files */
         /* Create output json */          
/*         
         run writejsonfile(input hresult,
                  input v_infile + ".json",
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  */
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
                         
                        
       output close.   
       
                         
  {us/gp/gpselout.i &printType = "printer"
   &printWidth = 132
   &pagedFlag = " "
   &stream = " "
   &appendToFile = " "
   &streamedOutputToTerminal = " "
   &withBatchOption = "yes"
   &displayStatementType = 1
   &withCancelMessage = "yes"
   &pageBottomMargin = 6
   &withEmail = "yes"
   &withWinprint = "yes"
   &defineVariables = "yes"}
   
  for each ttsupplierrs.
  
  export ttsupplierrs.
  
  end.

 
  for each api_status.
     
     export api_status.
  
  end.
  

   {us/mf/mfrtrail.i}

   
   
end. /* repeat on error undo mainloop, retry mainloop: */
